package com.cff.service.user;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.cff.domain.User;
import com.cff.domain.UserType;
import com.cff.mapper.user.UserMapper;
import com.cff.mapper.user.UserTypeMapper;
import com.cff.util.StringUtil;

@Service
public class UserService {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	JdbcTemplate jdbcTemplate;
	@Autowired
	UserMapper userMapper;
	
	@Autowired
	UserTypeMapper userTypeMapper;
	
	/**
	 * 检查登陆id是否存在
	 */
	public Boolean checkLoginIdUsed(String loginId){
		User user = userMapper.getUser(loginId);
		if(user != null){
			logger.info("该用户登录id已存在");
			return true;
		}else{
			logger.info("可以注册");
		}
		return false;
		
	}

	public Boolean register(User user) {
		String sql = "insert into cff_user values("+user.getLoginId()+","+user.getPasswd()+")";
		jdbcTemplate.execute(sql);
		return true;
	}
	
	public Boolean login(User user) {
		logger.info(user.toString());
		User userExist = userMapper.getUser(user.getLoginId());
		if(userExist != null){
			logger.info("查询到用户{}",user.getLoginId());
			if(user.getPasswd().equals(userExist.getPasswd())){
				return true;
			}
			return false;
		}else{
			logger.info("查无此用户");
			return false;
		}
	}

	public Boolean modify(User user) {
		int count = userMapper.modifyPasswd(user);
		if(count > 0)return true;
		return false;
	}
	
	public String findUserType(String userId){
		UserType userType = userTypeMapper.findUserType(userId);
		if(userType == null)return null;
		if(StringUtil.isNotEmpty(userType.getUserType())){
			if("1001".equals(userType.getUserType())){
				return "CustomerServiceApproval";
			}else if("1000".equals(userType.getUserType())){
				return "ManagerApproval";
			}else{
				return "commit";
			}
		}
		return null;
	}
	
}
